package com.puboot.module.blog.controller;

import com.puboot.common.util.Constants;
import com.puboot.common.util.GasDCUtil;
import com.puboot.common.util.ResultUtil;
import com.puboot.module.admin.vo.base.ResponseVo;
import com.puboot.module.blog.model.Customer;
import com.puboot.module.blog.model.EghgOne;
import com.puboot.module.blog.model.EghgTwo;
import com.puboot.module.blog.model.EghgTwoDetail;
import com.puboot.module.blog.service.EghgOneService;
import com.puboot.module.blog.service.EghgTwoDetailService;
import com.puboot.module.blog.service.EghgTwoService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blog/eghg/api")
@AllArgsConstructor
public class EghgController {

    private final HttpServletRequest httpServletRequest;
    private final EghgOneService eghgOneService;
    private final EghgTwoService eghgTwoService;
    private final EghgTwoDetailService eghgTwoDetailService;

    @PostMapping("/eghgone")
    public ResponseVo eghgone(EghgOne eghgOne) {
        try {
            HttpSession httpSession = httpServletRequest.getSession();
            Customer customer = (Customer) httpSession.getAttribute("customer");
            Double eghg = GasDCUtil.getTotalGreenhouseGasEmissions(eghgOne.getEco2Burn(), eghgOne.getEghgProcess(),
                    eghgOne.getEghgRecovery(), eghgOne.getEghgNetElectricity(), eghgOne.getEghgNetHeat());
            eghgOne.setResult(GasDCUtil.getThousandth(eghg));
            eghgOne.setCreateTime(new Date());
            eghgOne.setCustomerId(customer.getId());
            eghgOne.setState(Constants.STATE_VAILD);
            eghgOneService.save(eghgOne);
            return ResultUtil.success("计算成功", eghgOne);
        } catch (Exception e) {
            log.error("计算失败", e);
            return ResultUtil.error("计算失败");
        }
    }

    @PostMapping("/eghgtwo")
    public ResponseVo eghgtwo(EghgTwo eghgTwo) {
        try {
            HttpSession httpSession = httpServletRequest.getSession();
            Customer customer = (Customer) httpSession.getAttribute("customer");
            List<String> adis = Arrays.asList(eghgTwo.getAdi().split(","));
            List<String> ccis = Arrays.asList(eghgTwo.getCci().split(","));
            List<String> ofis = Arrays.asList(eghgTwo.getOfi().split(","));
            BigDecimal eco2Burn = GasDCUtil.getEco2Burn(adis, ccis, ofis);
            Double eghg = GasDCUtil.getTotalGreenhouseGasEmissions(eco2Burn.doubleValue(), eghgTwo.getEco2RawMaterial(), eghgTwo.getEco2Nitrate(),
                    eghgTwo.getEn2oNitricAcid(), eghgTwo.getEn2oAdipicAcid(), eghgTwo.getQ(),
                    eghgTwo.getPurCo2(), eghgTwo.getAdPower(), eghgTwo.getEfPower(),
                    eghgTwo.getAdHeat(), eghgTwo.getEfHeat());
            eghgTwo.setResult(GasDCUtil.round(eghg, 2));
            eghgTwo.setEco2Burn(eco2Burn.doubleValue());
            eghgTwo.setCreateTime(new Date());
            eghgTwo.setCustomerId(customer.getId());
            eghgTwo.setState(Constants.STATE_VAILD);
            eghgTwoService.save(eghgTwo);
            for (int i = 0; i < adis.size(); i++) {
                EghgTwoDetail eghgTwoDetail = new EghgTwoDetail();
                eghgTwoDetail.setFossilFuelAdi(Double.valueOf(adis.get(i)));
                eghgTwoDetail.setOfi(Double.valueOf(ofis.get(i)));
                eghgTwoDetail.setCci(Double.valueOf(ccis.get(i)));
                eghgTwoDetail.setEghgTwoId(eghgTwo.getId());
                eghgTwoDetailService.save(eghgTwoDetail);
            }
            return ResultUtil.success("计算成功", eghgTwo);
        } catch (Exception e) {
            log.error("计算失败", e);
            return ResultUtil.error("计算失败");
        }
    }

    @PostMapping("/eghgThree")
    public ResponseVo eghgThree(EghgTwo eghgTwo) {
        try {
            HttpSession httpSession = httpServletRequest.getSession();
            Customer customer = (Customer) httpSession.getAttribute("customer");
            List<String> fossilFuelAdis = new ArrayList<>();
            if (StringUtils.isNotBlank(eghgTwo.getFossilFuelAdi())) {
                fossilFuelAdis.addAll(Arrays.asList(eghgTwo.getFossilFuelAdi().split(",")));
            }
            List<String> ncvis = new ArrayList<>();
            if (StringUtils.isNotBlank(eghgTwo.getNcvi())) {
                ncvis.addAll(Arrays.asList(eghgTwo.getNcvi().split(",")));
            }
            List<String> fuelVarietyEfis = new ArrayList<>();
            if (StringUtils.isNotBlank(eghgTwo.getFuelVarietyEfi())) {
                fuelVarietyEfis.addAll(Arrays.asList(eghgTwo.getFuelVarietyEfi().split(",")));
            }
            List<String> ofis = new ArrayList<>();
            if (StringUtils.isNotBlank(eghgTwo.getOfi())) {
                ofis.addAll(Arrays.asList(eghgTwo.getOfi().split(",")));
            }
            List<String> adrs = new ArrayList<>();
            if (StringUtils.isNotBlank(eghgTwo.getAdr())) {
                adrs.addAll(Arrays.asList(eghgTwo.getAdr().split(",")));
            }
            List<String> ccrs = new ArrayList<>();
            if (StringUtils.isNotBlank(eghgTwo.getCcr())) {
                ccrs.addAll(Arrays.asList(eghgTwo.getCcr().split(",")));
            }
            List<String> adps = new ArrayList<>();
            if (StringUtils.isNotBlank(eghgTwo.getAdp())) {
                adps.addAll(Arrays.asList(eghgTwo.getAdp().split(",")));
            }
            List<String> ccps = new ArrayList<>();
            if (StringUtils.isNotBlank(eghgTwo.getCcp())) {
                ccps.addAll(Arrays.asList(eghgTwo.getCcp().split(",")));
            }
            List<String> adws = new ArrayList<>();
            if (StringUtils.isNotBlank(eghgTwo.getAdw())) {
                adws.addAll(Arrays.asList(eghgTwo.getAdw().split(",")));
            }
            List<String> ccws = new ArrayList<>();
            if (StringUtils.isNotBlank(eghgTwo.getCcw())) {
                ccws.addAll(Arrays.asList(eghgTwo.getCcw().split(",")));
            }
            List<String> nitrateAdis = new ArrayList<>();
            if (StringUtils.isNotBlank(eghgTwo.getNitrateAdi())) {
                nitrateAdis.addAll(Arrays.asList(eghgTwo.getNitrateAdi().split(",")));
            }
            List<String> nitrateEfis = new ArrayList<>();
            if (StringUtils.isNotBlank(eghgTwo.getNitrateEfi())) {
                nitrateEfis.addAll(Arrays.asList(eghgTwo.getNitrateEfi().split(",")));
            }
            List<String> puris = new ArrayList<>();
            if (StringUtils.isNotBlank(eghgTwo.getPuri())) {
                puris.addAll(Arrays.asList(eghgTwo.getPuri().split(",")));
            }
            List<String> nitricAcidAdjs = new ArrayList<>();
            if (StringUtils.isNotBlank(eghgTwo.getNitricAcidAdj())) {
                nitricAcidAdjs.addAll(Arrays.asList(eghgTwo.getNitricAcidAdj().split(",")));
            }
            List<String> nitricAcidEfjs = new ArrayList<>();
            if (StringUtils.isNotBlank(eghgTwo.getNitrateEfi())) {
                nitricAcidEfjs.addAll(Arrays.asList(eghgTwo.getNitricAcidEfj().split(",")));
            }
            List<String> nitricAcidNscrs = new ArrayList<>();
            if (StringUtils.isNotBlank(eghgTwo.getNitrateEfi())) {
                nitricAcidNscrs.addAll(Arrays.asList(eghgTwo.getNitricAcidNscr().split(",")));
            }
            List<String> nitricAcidOthers = new ArrayList<>();
            if (StringUtils.isNotBlank(eghgTwo.getNitricAcidOther())) {
                nitricAcidOthers.addAll(Arrays.asList(eghgTwo.getNitricAcidOther().split(",")));
            }
            List<String> oxalicAcidAdjs = new ArrayList<>();
            if (StringUtils.isNotBlank(eghgTwo.getOxalicAcidAdj())) {
                oxalicAcidAdjs.addAll(Arrays.asList(eghgTwo.getOxalicAcidAdj().split(",")));
            }
            List<String> oxalicAcidEfjs = new ArrayList<>();
            if (StringUtils.isNotBlank(eghgTwo.getOxalicAcidEfj())) {
                oxalicAcidEfjs.addAll(Arrays.asList(eghgTwo.getOxalicAcidEfj().split(",")));
            }
            List<String> oxalicAcidNscrs = new ArrayList<>();
            if (StringUtils.isNotBlank(eghgTwo.getOxalicAcidNscr())) {
                oxalicAcidNscrs.addAll(Arrays.asList(eghgTwo.getOxalicAcidNscr().split(",")));
            }
            List<String> oxalicAcidOthers = new ArrayList<>();
            if (StringUtils.isNotBlank(eghgTwo.getOxalicAcidOther())) {
                oxalicAcidOthers.addAll(Arrays.asList(eghgTwo.getOxalicAcidOther().split(",")));
            }
            BigDecimal eco2Burn = GasDCUtil.getEco2Burn(fossilFuelAdis, ofis, ncvis, fuelVarietyEfis);
            BigDecimal eco2RawMaterial = GasDCUtil.getEco2RawMaterialByAdrAndCcrAndAdpAndCcpAndAdwAndCcw(adrs, ccrs, adps,
                    ccps, adws, ccws);
            BigDecimal eco2CarBonate = GasDCUtil.getEco2CarbonateByAdiAndEfiAndPuri(nitrateAdis, nitrateEfis, puris);
            BigDecimal eco2NitricAcid = GasDCUtil.getEn2oNitricAcidByAdjAndEfjAndNscrAndOther(nitricAcidAdjs, nitricAcidEfjs, nitricAcidNscrs, nitricAcidOthers);
            BigDecimal en2oAdipicAcid = GasDCUtil.getEn2oAdipicAcidByAdjAndEfjAndNscrAndOther(oxalicAcidAdjs, oxalicAcidEfjs, oxalicAcidNscrs, oxalicAcidOthers);
            Double eghg = GasDCUtil.getTotalGreenhouseGasEmissions(eco2Burn.doubleValue(), eco2RawMaterial.doubleValue(), eco2CarBonate.doubleValue(),
                    eco2NitricAcid.doubleValue(), en2oAdipicAcid.doubleValue(), eghgTwo.getQ(),
                    eghgTwo.getPurCo2(), eghgTwo.getAdPower(), eghgTwo.getEfPower(),
                    eghgTwo.getAdHeat(), eghgTwo.getEfHeat());
            eghgTwo.setEco2Burn(eco2Burn.doubleValue());
            eghgTwo.setEco2RawMaterial(eco2RawMaterial.doubleValue());
            eghgTwo.setEco2Nitrate(eco2CarBonate.doubleValue());
            eghgTwo.setEn2oNitricAcid(eco2NitricAcid.doubleValue());
            eghgTwo.setEn2oAdipicAcid(en2oAdipicAcid.doubleValue());
            eghgTwo.setResult(GasDCUtil.round(eghg, 2));
            eghgTwo.setCreateTime(new Date());
            eghgTwo.setCustomerId(customer.getId());
            eghgTwo.setState(Constants.STATE_VAILD);
            eghgTwoService.save(eghgTwo);
            int[] sizeArr = new int[]{fossilFuelAdis.size(), ncvis.size(), fuelVarietyEfis.size(), ofis.size(), adrs.size(), ccrs.size(),
                    adps.size(), ccps.size(), adws.size(), ccws.size(), nitrateAdis.size(), nitrateEfis.size(), puris.size(), nitricAcidAdjs.size(), nitricAcidEfjs.size(), nitricAcidNscrs.size(), nitricAcidOthers.size(), oxalicAcidAdjs.size(), oxalicAcidEfjs.size(), oxalicAcidNscrs.size(), oxalicAcidOthers.size()};
            Arrays.sort(sizeArr);
            int sizt = sizeArr[sizeArr.length - 1];
            for (int i = 0; i < sizt; i++) {
                EghgTwoDetail eghgTwoDetail = new EghgTwoDetail();
                if (fossilFuelAdis.size() > i) {
                    eghgTwoDetail.setFossilFuelAdi(Double.valueOf(fossilFuelAdis.get(i)));
                }
                if (ncvis.size() > i) {
                    eghgTwoDetail.setNcvi(Double.valueOf(ncvis.get(i)));
                }
                if (fuelVarietyEfis.size() > i) {
                    eghgTwoDetail.setFuelVarietyEfi(Double.valueOf(fuelVarietyEfis.get(i)));
                }
                if (ofis.size() > i) {
                    eghgTwoDetail.setOfi(Double.valueOf(ofis.get(i)));
                }
                if (adrs.size() > i) {
                    eghgTwoDetail.setAdr(Double.valueOf(adrs.get(i)));
                }
                if (ccrs.size() > i) {
                    eghgTwoDetail.setCcr(Double.valueOf(ccrs.get(i)));
                }
                if (adps.size() > i) {
                    eghgTwoDetail.setAdp(Double.valueOf(adps.get(i)));
                }
                if (ccps.size() > i) {
                    eghgTwoDetail.setCcp(Double.valueOf(ccps.get(i)));
                }
                if (adws.size() > i) {
                    eghgTwoDetail.setAdw(Double.valueOf(adws.get(i)));
                }
                if (ccws.size() > i) {
                    eghgTwoDetail.setCcw(Double.valueOf(ccws.get(i)));
                }
                if (nitrateAdis.size() > i) {
                    eghgTwoDetail.setNitrateAdi(Double.valueOf(nitrateAdis.get(i)));
                }
                if (nitrateEfis.size() > i) {
                    eghgTwoDetail.setNitricAcidEfj(Double.valueOf(nitrateEfis.get(i)));
                }
                if (puris.size() > i) {
                    eghgTwoDetail.setPuri(Double.valueOf(puris.get(i)));
                }
                if (nitricAcidAdjs.size() > i) {
                    eghgTwoDetail.setNitricAcidAdj(Double.valueOf(nitricAcidAdjs.get(i)));
                }
                if (nitricAcidEfjs.size() > i) {
                    eghgTwoDetail.setNitricAcidEfj(Double.valueOf(nitricAcidEfjs.get(i)));
                }
                if (nitricAcidNscrs.size() > i) {
                    eghgTwoDetail.setNitricAcidNscr(Double.valueOf(nitricAcidNscrs.get(i)));
                }
                if (nitricAcidOthers.size() > i) {
                    eghgTwoDetail.setNitricAcidOther(Double.valueOf(nitricAcidOthers.get(i)));
                }
                if (oxalicAcidAdjs.size() > i) {
                    eghgTwoDetail.setOxalicAcidAdj(Double.valueOf(oxalicAcidAdjs.get(i)));
                }
                if (oxalicAcidEfjs.size() > i) {
                    eghgTwoDetail.setOxalicAcidEfj(Double.valueOf(oxalicAcidEfjs.get(i)));
                }
                if (oxalicAcidNscrs.size() > i) {
                    eghgTwoDetail.setOxalicAcidNscr(Double.valueOf(oxalicAcidNscrs.get(i)));
                }
                if (oxalicAcidOthers.size() > i) {
                    eghgTwoDetail.setOxalicAcidOther(Double.valueOf(oxalicAcidOthers.get(i)));
                }
                eghgTwoDetail.setEghgTwoId(eghgTwo.getId());
                eghgTwoDetailService.save(eghgTwoDetail);
            }
            return ResultUtil.success("计算成功", eghgTwo);
        } catch (Exception e) {
            log.error("计算失败", e);
            return ResultUtil.error("计算失败");
        }
    }
}
